//      search.js
//      
//      Copyright 2010 Fedorchenko Alexander <admin@xepec.ru>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.



//function setlocation(set_q)
//{
	//obra = location.href.split("#");
	//if (obra[1])
	//{
		//query1 = obra[1].split("&");
		//for (var i=0; i < query1.length;i++)
		//{
			//param = query1[i].split("=");
			//if (param[0] == "s")
			//{
				//query = param[1];
			//}
			//else
			//{
				//query = set_q;
			//}
		//}
		
	//}
	//else
	//{
		//location.href = location.href + '#s=' +query+'';
	//}
//}


// 
// name: setlocationparam
// @param
// @return
function setlocationparam(param_q,set_q)
{
	
	var obra = location.href.split("#");
	var query = "";
	if (obra[1])
	{
		var query1 = obra[1].split("&");
		var count_param_q = query1.length;
		for (var i=0; i < query1.length;i++)
		{
			param = query1[i].split("=");
			if (param[0] != "")
			{
				if (param[0] == param_q)
				{
					query = query + param_q+"="+set_q+"&";
				}
				else
				{
					count_param_q = count_param_q-1;
					//alert(i);
					query = query+param[0]+"="+param[1]+"&";
				}
			}
			else
			{
				count_param_q = count_param_q-1;
			}
			//query = query+param[0]+"="+param[1]+"&";
		}
		
	}
	else
	{
		query = query+param_q+"="+set_q+"&";
	}
	if(!count_param_q){query = query + param_q+"="+set_q+"&";}
	//alert("кол-во ключей"+param_q+"="+count_param_q);
	location.href = obra[0]+"#"+query;
}

// 
// name: shadow
// @param
// @return
function shadow(show,hide)
{
				document.getElementById(hide).style.display = 'none';
				document.getElementById(show).style.display = '';
}
	
// 
// name: checklocation
// @param
// @return
function checklocation()
{
	obra = location.href.split("#");
	if (obra[1])
	{
		GetXMLFile("engine.pl"+obra[1], ParseXML);
	}
	else
	{
		//location.href = location.href + '#s=' +query+'';
		shadow('search_form_shadow','search_button_shadow');
	}
}

// 
// name: send_query
// @param
// @return
function send_query()
{
	var query = '';
	var email = document.getElementById("email").value;	if (email){query = query +"email="+email+"&";}
	var nic = document.getElementById("nic").value;	if (nic){query = query +"nic="+nic+"&";}
	var name = document.getElementById("name").value;	if (name){query = query +"name="+name+"&";}
	var sexw = document.getElementById("sexw").checked;	if (sexw){query = query +"sex=8&";}
	var sexm = document.getElementById("sexm").checked;	if (sexm){query = query +"sex=1&";}
	var age_t = document.getElementById("age_t").value;	if (age_t){query = query +"age_t="+age_t+"&";}			
	var age = document.getElementById("age").value;	if (age){query = query +"age="+age+"&";}
	var foto = document.getElementById("foto").checked;	if (foto){query = query +"foto=1&";}
	
	
	/////////////////////////
	//alert(query);
	obra = location.href.split("#");
	location.href = obra[0]+"#"+query;
	shadow('search_button_shadow','search_form_shadow');
}

function print_search_error(code)
{
		switch(code)
		{
			case "1":
			  return "<h2>Поиск не дал результатов</h2>";
			  break;
			case "2":
			 return "<h2>Эй, у нас не так много страниц</h2>";
			  break;
			default:
			return "";
		}
}

// 
// name: result_print
// @param
// @return
function result_print(test,text)
{
	document.getElementById(test).innerHTML = document.getElementById(test).innerHTML + text;
}
	
// 
// name: ParseXML
// @param
// @return
function ParseXML(Response)
{
	var doc = Response.responseXML.documentElement;
	//document.getElementById("search_q").innerHTML = doc.getElementsByTagName("queryg").item(0).firstChild.nodeValue;
	
	var error_code = doc.getElementsByTagName("result")[0].getAttribute("error_code");
	
	var result_count = doc.getElementsByTagName("result")[0].getAttribute("items");
	var start_position = doc.getElementsByTagName("result")[0].getAttribute("start_position");
		
	//alert("error_code:"+error_code);
	if(error_code != 0){sss_head = print_search_error(error_code);}
	else
	{
		var sss_head = '<h1>Результат поиска</h1>';
		
		sss_head = sss_head + '<h2 style="display: inline;clear:both;">По Вашему запросу найденно: <lable id="search_q"><b>'+result_count+'</b></lable> результатов</h2>';
		sss_head = sss_head +'<lable style="float: right; display: inline;clear:both;">Сортировать по:';
			sss_head = sss_head + '<select id="sort" onchange="setlocationparam(\'sort\',getElementById(\'sort\').value)" ><option value=\"pepyaka\">Рейтингу</option><option value=\"bday\">Возрасту</option><option value=\"name\">Имени</option><option value=\"last_time\">Последнему посещению</option></select>';
		//	sss_head = sss_head + '<input type="submit" onclick="setlocationparam(\'sort\',getElementById(\'sort\').value)" name="send" id="send" value="Sort" />';
		sss_head = sss_head +' </lable>';
	}
	
	var sss = '';
	//Проходимся по всем элементам-записям и  составляем их репрезентацию
	var items = doc.getElementsByTagName("item");
	for (var i = 0; i< items.length; i++)
	
	{
		
		sss = sss + '<li id="result'+i+'" >';
		sss = sss + '';
		  sss = sss + '<div id="result'+i+'">';
			if (items[i].getAttribute("foto") == 0)
			{
				sss = sss +'<a href="/username/?uname=' + items[i].getAttribute("id")+'" >'+'<img class="ava" align="left" width="100px" src="/usr/no_ava.gif" />'+'</a>';
			}
			else
			{
				sss = sss +'<a href="/username/?uname=' + items[i].getAttribute("id")+'" >'+'<img class="ava" align="left" width="100px" src="/usr/' + items[i].getAttribute("id")+'/userpic-small.png" />'+'</a>';
			}
			if (items[i].getAttribute("sex") == 1)
			{sss = sss + '<span id="sex_boy">♂</span>';}
			if (items[i].getAttribute("sex") == 8)
			{sss = sss + '<span id="sex_girl">♀</span>';}
			
			sss = sss +'<lable style="float: right; display: inline;  margin: 0px 5px 0px 0px;">&nbsp;Возраст:'+ items[i].getAttribute("age")+' </lable>';	
			sss = sss +'<lable style="float: right; display: inline; ">Рейтинг:'+ items[i].getAttribute("pepyaka")+' </lable>';					
			sss = sss +'&#160;<a href="/username/?uname=' + items[i].getAttribute("id")+'" >'+items[i].getAttribute("nic")+'</a>';
			sss = sss + '<br />';
			sss = sss +'<lable> '+ items[i].getAttribute("name")+' </lable>';
				
			

			sss = sss + '</div>';
		sss = sss + '</li>';
	}
	page_count = doc.getElementsByTagName("result")[0].getAttribute("pages");
	pages_html = '<div align="center"><';
	for(var i=1;i<=page_count;i++)
	{
			pages_html = pages_html + '<a href="" onclick="setlocationparam(\'page\','+i+');">'+i+'</a>';
			if(i!=page_count){pages_html = pages_html + '|';}
	}
	pages_html = pages_html + "></div>";
	
	document.getElementById("search22").innerHTML = document.getElementById("search22").innerHTML + sss_head +'<ol start="'+start_position+'">'+sss+'</ol>'+pages_html;
	//alert(document.getElementById("search22").innerHTML);
}
	

$(document).ready(function() {
	$(window).bind( 'hashchange', function(){
		var hash = location.hash;
		// Iterate over all nav links, setting the "selected" class as-appropriate.
		//alert(hash.replace("#","?"));
			
		GetXMLFile("engine.pl"+hash.replace("#","?"), ParseXML);
		var search = document.getElementById('search22');
		search.innerHTML = "";
	})
	$(window).trigger( 'hashchange' );
});
//GetXMLFile("engine.pl?s="+query, ParseXML);
